//
//  User.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "User.h"

static User * share = nil;

@implementation User

+(instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[User alloc] init];
    });
    return share;
}

#pragma mark - Login
-(void)loginWithUserName:(NSString *)username withPassword:(NSString *)password
{
    [BmobUser loginWithUsernameInBackground:username password:password block:^(BmobUser *user, NSError *error) {
        if (error) {
            self.isLogin = NO;
            TRLog(@"error:%@",[error description]);
        }else{
            self.isLogin = YES;
            TRLog(@"登录成功");
        }
        //在block执行结束再去执行代理
        if (self.delegate && [self.delegate respondsToSelector:@selector(judgeUserLoginStateWithUser:withIsLogin:)]) {
            [self.delegate judgeUserLoginStateWithUser:self withIsLogin:self.isLogin];
        }
    }];
    
    
}
#pragma mark - Register
-(void)registWithUserName:(NSString *)username withPassword:(NSString *)password withEmail:(NSString *)email withMobilePhoneNumber:(NSString *)mobilePhoneNumber
{
    BmobUser * bUser = [[BmobUser alloc] init];
    [bUser setUsername:username];
    [bUser setPassword:password];
    [bUser setEmail:email];
    [bUser setMobilePhoneNumber:mobilePhoneNumber];
    [bUser setObject:nil forKey:LAST_POST];
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            TRLog(@"创建User成功");
        }else{
            NSLog(@"创建User失败");
            if (error) {
                TRLog(@"%@",[error description]);
            }
        }
    }];
}
@end
